﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainTicketBookingSystem
{
    public partial class BuyTicketForm : Form
    {
        public static BuyTicketForm buyTicketForm;
        public static string selectedTS;
        private CarriageService carriageService = new CarriageService();
        private StationService stationService = new StationService();
        private TrainSequenceService trainSequenceService = new TrainSequenceService();
        private List<Station> stationsList = null;
        private List<TrainSequence> trainSequencesList = null;
        private DataGridViewRow selectedRow = null;
        public BuyTicketForm()
        {
            InitializeComponent();
            buyTicketForm = this;
            stationsList = stationService.FindAllStation();
        }

        private void BuyTicketForm_Load(object sender, EventArgs e)
        {

        }

        private void BuyTicketForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            UserForm.userForm.Show();         
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnBuyTicket_Click(object sender, EventArgs e)
        {
            if(selectedRow == null)
            {
                MessageBox.Show("请选择要购买的票");
            }
            else
            {
                this.Hide();
                MainBuy mainBuy = new MainBuy();
                mainBuy.Show();
            }
        }

        private void BtnSearch_Click(object sender, EventArgs e)
        {
            if (trainSequencesList != null)
                trainSequencesList.Clear();

            dataTable.Rows.Clear();

            DateTime LaunchTime = dateTimePicker.Value;
            string StartStation = comboBoxStartStation.Text;
            string EndStation = comboBoxEndStation.Text;
            trainSequencesList = trainSequenceService.FindTrainSqeuenceByTimeAndStation(LaunchTime, StartStation, EndStation);

            this.textBoxCount.Text = trainSequencesList.Count.ToString();
            ShowInTable();
        }

        private void ShowInTable()
        {
            if (trainSequencesList.Count == 0)
            {
                return;
            }
            this.dataTable.RowCount = trainSequencesList.Count;

            for (int i = 0; i < trainSequencesList.Count; i++)
            {
                TrainSequence ts = trainSequencesList[i];
                this.dataTable[0, i].Value = ts.TrainNum;
                this.dataTable[1, i].Value = ts.TrainId;
                this.dataTable[2, i].Value = comboBoxStartStation.Text;
                this.dataTable[3, i].Value = comboBoxEndStation.Text;
                this.dataTable[4, i].Value = ts.LaunchTime;
                this.dataTable[5, i].Value = ts.ArriveTime;
                this.dataTable[6, i].Value = ts.TravelTime;
                this.dataTable[7, i].Value = carriageService.FindHighSeatNumByTrainId(ts.TrainId);
                this.dataTable[8, i].Value = carriageService.FindLowSeatNumByTrainId(ts.TrainId);
            }
        }

        private void dataTable_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                selectedRow = dataTable.Rows[e.RowIndex];
                selectedTS = selectedRow.Cells[0].Value.ToString();
            }
        }
    }
}
